import axios from "axios";
import QS from "qs";
import { Loading, MessageBox } from "element-ui";
import { getCookie, delCookie } from "@/assets/js/cookie";
import router from '@/router'

//dev
const url = 'http://127.0.0.1:8001'
if (process.env.NODE_ENV == 'development') {
    axios.defaults.baseURL = url;
} else if (process.env.NODE_ENV == 'debug') {
    axios.defaults.baseURL = '';
}  else if (process.env.NODE_ENV == 'production') {
    if (process.env.VUE_APP_TITLE == 'sit') {
        axios.defaults.baseURL = 'http://47.96.153.224:8081'; //sit
    } else if (process.env.VUE_APP_TITLE == 'dev') {
        axios.defaults.baseURL = 'http://47.96.14.108:8081'; //dev
    } else if (process.env.VUE_APP_TITLE == 'uat') {
        axios.defaults.baseURL = 'http://121.196.222.245:8081'; //uat
    }  else if (process.env.VUE_APP_TITLE == 'product1'){
        axios.defaults.baseURL = 'http://47.103.43.223:8081'; //生产
    }else{
        axios.defaults.baseURL = 'http://47.103.75.42:8081'; //生产2
    }
    //axios.defaults.baseURL = 'http://121.196.222.245:8081'; //准生产
    //axios.defaults.baseURL = 'http://101.37.66.72:8081';
    //axios.defaults.baseURL = 'http://47.96.14.108:8081'; //dev
    //axios.defaults.baseURL = 'http://47.96.153.224:8081'; //sit
}

axios.defaults.timeout = 420000
var loadinginstance;

axios.interceptors.request.use(
    config => {
        loadinginstance = Loading.service({
            lock: true,
            text: "加载中",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.6)",
            customClass: "osloading",
            fullscreen: true
        });
        if (config.url === "/batoauth2/oauth/token") {
            delCookie("hjLoginToken");
            delCookie("operatorId");
            delCookie("roleId");
            console.log(axios.defaults.baseURL)
        }

        if (getCookie("hjLoginToken")) {
            config.headers["accesstoken"] = getCookie("hjLoginToken");
        }

        config.headers['Content-Type'] = 'application/json;charset=UTF-8';


        return config;
    },
    error => {
        loadinginstance.close();
        return Promise.error(error);
    })
let count = 0;
let handleError = function (response) {
    var message = null;
    if (response && response.code) {
        message=response.message?response.message:""
        switch (response.code) {
            case "500": message = response.message; break;
            case "400": message = response.message; break;
            case "300": message = response.message; break;
            case "401":
                delCookie("hjLoginToken");
                delCookie("operatorId");
                delCookie("roleId");
                message = response.message;
                router.replace({
                    path: '/login'
                    // query: {
                    //     redirect: router.currentRoute.fullPath
                    // }
                });
                break;

        }

        count += 1;
        if (count == 1) {
            count = 0;
            MessageBox({
                title: "提示",
                message: message ? message : "加载失败",
                confirmButtonClass: "confirmButtonClass"
            });
        }
    }

}


axios.interceptors.response.use(
    response => {
        loadinginstance.close();
        if (response && response.status === 200) {
                return Promise.resolve(response.data);

            //  if (!res.data) {
            //     return Promise.resolve(response.data);
            // }
        } else {
            console.log("error1")
            handleError(response);
            return Promise.reject(response);
        }
    },

    error => {
        loadinginstance.close();
        console.log("error2")
        if (error && error.meta) {
            handleError(error.meta);
            return Promise.reject(error.meta);
        } else {
            handleError(error);
            return Promise.reject(error);
        }

    }
);
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function $get(url, params, blob) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,
            responseType: blob,
        })
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err)
            })
    });
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function $post(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, QS.stringify(params))
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err)
            })
    });
}

export function $postObj(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err)
            })
    });
}

export function $delete(url, params) {
    return new Promise((resolve, reject) => {
        axios.delete(url, QS.stringify(params))
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err)
            })
    });
}

